//
//  StoreManage
//
//  Created by MC on 2018/2/6.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

protocol MCAddTicketChooseGoodsOrBrandViewControllerDelegate : NSObjectProtocol {
    func MCAddTicketChooseGoodsOrBrandViewControllerChoose(id:Int,name:String)
}

class MCAddTicketChooseGoodsOrBrandViewController: MCViewController {
    
    var type = 0   // 3品牌  5商品
    
    weak var delegate : MCAddTicketChooseGoodsOrBrandViewControllerDelegate?

    private var pageNumber = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    // MARK: - Network
    func sendNetworking() {
        
        var urlStr : String = ""
        if type == 5 {  // 商品
            urlStr = JAVA_getStoreGoods
        } else {
            urlStr = JAVA_getStoreBrands
        }
        
        
        let para = [
            "pageNum" : pageNumber,
            ] as [String : Any]
        
        MCNetwork_post(url: urlStr, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()

            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
            }
            
            let dataArray = response["data"] as? NSArray ?? []
            for dict in dataArray {
                let model = MCAddTicketChooseGoodsOrBrandModel()
                model.setValuesForKeys(dict as! [String:Any])
                self.dataArrayM.add(model)
            }
            self.tableView.reloadData()
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        if type == 5 {
            self.title = "选择商品"
        } else {
            self.title = "选择品牌"
        }
        self.view.backgroundColor = UIColor.white
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
    }
    
    func initUI() {
        
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }
    }
    
    // MARK: - Setter & Getter
    lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

extension MCAddTicketChooseGoodsOrBrandViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCAddTicketChooseGoodsOrBrandCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.model = self.dataArrayM[indexPath.row] as! MCAddTicketChooseGoodsOrBrandModel
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = self.dataArrayM[indexPath.row] as! MCAddTicketChooseGoodsOrBrandModel
        
        if type == 5 {
            self.delegate?.MCAddTicketChooseGoodsOrBrandViewControllerChoose(id: model.goodsId, name: model.goodsName)
        } else {
            self.delegate?.MCAddTicketChooseGoodsOrBrandViewControllerChoose(id: model.brandId, name: model.brandName)
        }
        self.navigationController?.popViewController(animated: true)
    }
}


